import { Color } from '../core/math/Color'
import { Vector3 } from '../core/math/Vector3'
import { is2DArray } from '../core/utils'
import { Action } from './Action'
/**
 * 单个粒子类
 */
const DEG2RAD = Math.PI / 180
export class SingleParticle {
    /**
     * 构造函数
     */
    constructor() {
        this.position = new Vector3()
        this.velocity = new Vector3()
        this.acceleration = new Vector3()
        this.angle = 0
        this.angleVelocity = 0
        this.angleAcceleration = 0
        this.size = 16.0
        this.color =[1.0,1.0,1.0]
        this.opacity = 1.0
        this.age = 0
        this.alive = 0
    }
    // 更新粒子的位置、颜色、年龄、透明度、角度等属性
    update(dt) {
        this.position.add(this.velocity.clone().multiplyScalar(dt))
        this.velocity.add(this.acceleration.clone().multiplyScalar(dt))
        this.angle += this.angleVelocity * DEG2RAD * dt
        this.angleVelocity += this.angleAcceleration * DEG2RAD * dt
        this.age += dt

        if (is2DArray(this.size)) {
            this._size = this.interpolate(this.size[0], this.size[1], this.age)
        }
        if (is2DArray(this.color)) {
          this._color=this.interpolateRGB(this.color[0],this.color[1],this.age)
        }
        if (is2DArray(this.opacity)) {
            this._opacity = this.interpolate(this.opacity[0], this.opacity[1], this.age)
        }
    }
    interpolate(times, values, targetTime) {
        // 检查输入参数的有效性  
        if (times.length !== values.length || times.length === 0) {
            throw new Error("Times and values must have the same length and cannot be empty.");
        }

        // 找到 targetTime 应该插入的位置  
        for (let i = 0; i < times.length - 1; i++) {
            if (targetTime >= times[i] && targetTime <= times[i + 1]) {
                // 进行线性插值  
                const t0 = times[i];
                const t1 = times[i + 1];
                const v0 = values[i];
                const v1 = values[i + 1];

                // 计算插值  
                const interpolatedValue = v0 + (v1 - v0) * ((targetTime - t0) / (t1 - t0));
                return interpolatedValue;
            }
        }

        // 如果 targetTime 在范围外，返回值的边界（选择返回第一个或最后一个值）  
        if (targetTime < times[0]) {
            return values[0];
        } else {
            return values[values.length - 1];
        }
    }
    interpolateRGB(times, values, targetTime) {
        // 检查输入参数的有效性  
        if (times.length !== values.length || times.length === 0) {
            throw new Error("Times and values must have the same length and cannot be empty.");
        }

        // 找到 targetTime 应该插入的位置  
        for (let i = 0; i < times.length - 1; i++) {
            if (targetTime >= times[i] && targetTime <= times[i + 1]) {
                // 进行线性插值  
                const t0 = times[i];
                const t1 = times[i + 1];
                const v0 = values[i];
                const v1 = values[i + 1];

                // 计算插值  
                const interpolatedValue = v0.map((start, channel) => {
                    const end = v1[channel];
                    return start + (end - start) * ((targetTime - t0) / (t1 - t0));
                });

                return interpolatedValue;
            }
        }

        // 如果 targetTime 在范围外，返回值的边界（选择返回第一个或最后一个值）  
        if (targetTime < times[0]) {
            return values[0];
        } else {
            return values[values.length - 1];
        }
    }
}
